package com.example.demo.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.entity.SysUser;
import com.example.demo.entity.TargetAndBackMonkeyPeople;
import com.example.demo.entity.TargetAndBackMonkeyPeople2;
import com.example.demo.form.TargetAndBackMonkeyPeopleForm;
import com.example.demo.service.SysUserService;
import com.example.demo.service.TargetAndBackMonkeyPeopleService;
import com.example.demo.service.TargetAndBackMonkeyPeopleService2;

@RestController
@RequestMapping(value = "/TargetAndBackMonkeyPeopleController")
public class TargetAndBackMonkeyPeopleController {
	
	@Autowired
	private TargetAndBackMonkeyPeopleService service2;
	@Autowired
	private TargetAndBackMonkeyPeopleService2 service1;
	@Autowired
	private SysUserService sysUserDAO;
	
	private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
	
	@RequestMapping(value = "/save")
	private Object save(TargetAndBackMonkeyPeopleForm Form) throws java.text.ParseException {
		Integer saleId1 = Form.getId();
		Date d= new Date();
		Date d1 = dateFormat1.parse("1999-01-01");
		TargetAndBackMonkeyPeople2 sale1= new TargetAndBackMonkeyPeople2();
		TargetAndBackMonkeyPeople2 Sale1= new TargetAndBackMonkeyPeople2();
		TargetAndBackMonkeyPeople sale2= new TargetAndBackMonkeyPeople();
		TargetAndBackMonkeyPeople Sale2= new TargetAndBackMonkeyPeople();
		SysUser myUser = sysUserDAO.findById(Form.getUserId());
		TargetAndBackMonkeyPeople2 t1=service1.findByDateAndUser(Form.getDate(),myUser);
		System.out.println(Form.getDate());
		if(t1==null) {
			Sale1.setDate(Form.getDate());
			SysUser user=sysUserDAO.findById(Form.getUserId());
			Sale1.setUser(user);
			t1 = service1.save(Sale1);
		}else {
			 sale1 = service1.findById(t1.getId());
			 sale1.setDate(Form.getDate());
				SysUser user=sysUserDAO.findById(Form.getUserId());
				Sale1.setUser(user);
				service1.save(sale1);
				List<TargetAndBackMonkeyPeople> list = service2.findByUser1(sale1);
				for (TargetAndBackMonkeyPeople TargetAndBackMonkeyPeople : list) {
					if(d1.compareTo(TargetAndBackMonkeyPeople.getBeginDate())==-1) {
						d1=TargetAndBackMonkeyPeople.getBeginDate();
						Sale2=TargetAndBackMonkeyPeople;
					}
				}
				Sale2.setEndDate(d);
				service2.save(Sale2);
		}
		BeanUtils.copyProperties(Form, sale2, "id");
		sale2.setUser1(t1);
		sale2.setBeginDate(d);
		service2.save(sale2);
		return new AjaxResult("数据保存成功");			
	}
	
	@RequestMapping(value = "/findByuserId")
	public Object findByuserId(TargetAndBackMonkeyPeopleForm form) throws ParseException {
		Integer userId = form.getUserId();
		Date date = form.getDate();
		Integer erBiaoId ;
		TargetAndBackMonkeyPeople2 user1 = service1.findByUserIdAndDate(userId,date);
		if(user1!=null) {
			erBiaoId = user1.getId();
			TargetAndBackMonkeyPeople Sale2= new TargetAndBackMonkeyPeople();
			Date d1 = dateFormat1.parse("1999-01-01");
			List<TargetAndBackMonkeyPeople> list = service2.findByUser1Id(erBiaoId);
			for (TargetAndBackMonkeyPeople TargetAndBackMonkeyPeople : list) {
				if(d1.compareTo(TargetAndBackMonkeyPeople.getBeginDate())==-1) {
					d1=TargetAndBackMonkeyPeople.getBeginDate();
					Sale2=TargetAndBackMonkeyPeople;
				}
			}
			return new AjaxResult(Sale2);
		}else {
			return new AjaxResult(false,"该人员还未录入");
		}

	}
	
}